package com.xiaoma.controller;

import com.xiaoma.pojo.LoginInfo;
import com.xiaoma.pojo.Result;
import com.xiaoma.pojo.User;
import com.xiaoma.service.UserService;
import com.xiaoma.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api")
public class LoginController{
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Result<LoginInfo> login(@RequestBody LoginInfo loginInfo) {
        String username = loginInfo.getUsername();
        String password = loginInfo.getPassword();
        User userByUsernameAndPassword = userService.getUserByUsernameAndPassword(username, password);
        // 返回用户信息，不返回密码(保护用户隐私)
        loginInfo.setPassword(null);
        if (userByUsernameAndPassword != null){
            loginInfo.setPermission(userByUsernameAndPassword.getPermission());
            //log.info("登录成功:{}", loginInfo);
            return Result.success(loginInfo);
        }
        return Result.error("用户名或密码错误");
    }

}
